//
//  BAAlbumViewController.m
//  BabyAlbum
//
//  Created by master on 17/2/13.
//  Copyright © 2017年 master. All rights reserved.
//

#import "BAAlbumViewController.h"
#import "BAAlbumScrollVC.h"
#import "BAAlbumListVC.h"

@interface BAAlbumViewController () <BAAlbumDataProtocol>

@property (nonatomic, strong) BAAlbumScrollVC *albumScrollVC;
@property (nonatomic, strong) BAAlbumListVC *albumListVC;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) UIView *topView;
//@property (nonatomic, strong) BAAlbumIndexManager *albumDataManager;

//@property (nonatomic, strong) NSArray<BAAlbumModel *> *dataSource;

@property (nonatomic) BOOL isListType;


@end

@implementation BAAlbumViewController

- (instancetype)init {
    self = [super initWithNode:[[ASDisplayNode alloc] init]];
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"成长清单";
//    self.albumDataManager = [[BAAlbumIndexManager alloc] init];
    _isListType = NO;
    self.title = @"成长清单";
    [self initNavi];
//    [self loadData];
//    [self addTopView];
    [self loadShowType];

//    [self.view addSubview:self.albumScrollVC.view];

    // Do any additional setup after loading the view from its nib.
}

- (void)loadData {
//    [self.albumScrollVC loadData:self.albumDataManager];
//    [self.albumListVC loadData:self.albumDataManager];
}


- (void)initNavi {
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
//    [rightButton setTitle:@"列表" forState:UIControlStateNormal];
    [rightButton setImage:[UIImage imageNamed:@"list_mode"] forState:UIControlStateNormal];
//    [rightButton setTitleColor:[UIColor colorWithRed:255/255.0 green:94/255.0 blue:82/255.0 alpha:1.0] forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    rightButton.titleLabel.textAlignment = NSTextAlignmentRight;
    [rightButton addTarget:self action:@selector(changeShowTypeAction) forControlEvents:UIControlEventTouchUpInside];
    self.rightButton = rightButton;
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButton;
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
    [leftButton setImage:[UIImage imageNamed:@"setting"] forState:UIControlStateNormal];
//    [leftButton setTitleColor:[UIColor colorWithRed:255/255.0 green:94/255.0 blue:82/255.0 alpha:1.0] forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    leftButton.titleLabel.textAlignment = NSTextAlignmentRight;
//    [leftButton addTarget:self action:@selector(changeShowTypeAction) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
}

- (BAAlbumScrollVC *)albumScrollVC {
    if(!_albumScrollVC) {
        _albumScrollVC = [[BAAlbumScrollVC alloc] init];
        _albumScrollVC.manager = self;
        _albumScrollVC.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);

//        [self addChildViewController:_albumScrollVC];
         [self.node addSubnode:_albumScrollVC];
    }
    return _albumScrollVC;
}

- (BAAlbumListVC *)albumListVC {
    if(!_albumListVC) {
        _albumListVC = [[BAAlbumListVC alloc] init];
        _albumListVC.manager = self;
        _albumListVC.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);

//        [self addChildViewController:_albumListVC];
        [self.node addSubnode:_albumListVC];

    }
    return _albumListVC;
}

- (void)changeShowTypeAction {
    _isListType = !_isListType;
    [UIView beginAnimations:@"change" context:nil];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationRepeatAutoreverses:NO];
    [self loadShowType];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
    [UIView commitAnimations];
}

//// 代理方法，检测动画介绍然后进行其他操作 还有其他两个方法
//- (void)animationDidStop:(NSString *)animationId finished:(NSNumber *)finished context:(void *)context
//{
//    [self changeShowType];
//}

- (void)loadShowType {
    BAALBUM_DATA.isList = _isListType;

    if(_isListType) {

        [self.node insertSubnode:self.albumListVC aboveSubnode:self.albumScrollVC];
        [self.rightButton setImage:[UIImage imageNamed:@"cover_mode"] forState:UIControlStateNormal];
        BAALBUM_DATA.showingCollectNode = self.albumListVC.collectionNode;

    } else {

        [self.node insertSubnode:self.albumScrollVC aboveSubnode:self.albumListVC];

        [self.rightButton setImage:[UIImage imageNamed:@"list_mode"] forState:UIControlStateNormal];
        BAALBUM_DATA.showingCollectNode = self.albumScrollVC.collectionNode;

    }
    [self.view bringSubviewToFront:self.topView];
    
}

@end
